﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour {
    public GameObject buttle;
    public Transform gunFirePos;
    public GameObject gun;

    [Range(0,5)]
    public float FireCoolTime = 0.5f;
    // Use this for initialization
    void Start () {
        Invoke("OnFire", FireCoolTime);
    }
	
	// Update is called once per frame
	void Update () {
        //GetComponent<Rigidbody2D>().AddForce(Vector2.left * 10, ForceMode2D.Impulse);        

        gun.transform.right=-(this.gameObject.OnFindTransform("Hero").position-this.gameObject.transform.position).Vector3Up();
      
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "Bullet")
        {
            print("被集中");
            Debug.Log(collision.offset);
            Vector2 dir = (transform.position - collision.transform.position).Vector3ToVector2();
            GetComponent<Rigidbody2D>().AddForce(dir * 10, ForceMode2D.Impulse);
        }

    }

    

    void OnFire()
    {
        print("e frie");
        //开火
        GameObject go = Instantiate(buttle);
        go.transform.position = gunFirePos.position;
        go.transform.rotation = gunFirePos.rotation;
    }

}
